<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-03 00:33:05
 * @LastEditTime: 2023-09-19 15:41:44
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Models;

use App\Traits\AttachmentTrait;
use App\Traits\AutoCodeTrait;
use App\Traits\SoftDeletesTrait;
use Illuminate\Support\Facades\App;

class FinanceVoucher extends BaseModel
    {
    use AutoCodeTrait;
    use AttachmentTrait;
    use SoftDeletesTrait;
    public $timestamps = true;
    public $table = "sys_finance_voucher";
    protected $guarded = [];
    //附件
    public $attachmentkey = "code";
    protected $casts = [];

    /**
     * 标识
     *
     * @var string
     */
    protected $ACSign = 'VOUCHER_CODE';

    /**
     * 编号所用的字段
     *
     * @var string
     */
    protected $ACCode = 'code';

    public function status()
        {
        return $this->belongsTo(Dic::class, "status_id", "key")->where(function ($q)
            {
            $q->where("code", 'Finance_Status');
            });
        }

    public function details()
        {
        return $this->hasMany(FinanceVoucherDetails::class, "main_id")->withTrashed();
        }
    public function getSourceAttribute()
        {
        return App::make($this->source_model)->where($this->source_key, "=", $this->source_value)->first();
        }
    public function _deleting($model)
        {
        $model->status_id = -1;
        $model->save();
        }
    }
